//
//  WatermarkEditCell.m
//  WatermarkCamera
//
//  Created by zhibao on 2023/9/13.
//

#import "WatermarkEditCell.h"

@interface WatermarkEditCell(){
    UISwitch *sw;
    UIButton *selectBtn;
    UILabel *titleLabel;
    UILabel *contentLabel;
}
@end
@implementation WatermarkEditCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        sw = [[UISwitch alloc]initWithFrame:CGRectMake(20, 18, 30, 18)];
        [sw setTransform:CGAffineTransformMakeScale(0.8, 0.8)];
        [sw addTarget:self action:@selector(swchange:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:sw];
        sw.onTintColor = MYBlue;
        sw.centerY = 55 / 2;
        
        titleLabel = [UILabel new];
        titleLabel.text = @"备注:";
        titleLabel.font = MY_SCALE_RegularFONT(13);
        titleLabel.textColor = MYBlock;
        [self.contentView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(80);
            make.centerY.equalTo(self.contentView);
        }];
        
        selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [selectBtn setImage:[UIImage imageNamed:@"arrow_right"] forState:UIControlStateNormal];
        [self.contentView addSubview:selectBtn];
        selectBtn.userInteractionEnabled = NO;
        [selectBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10);
            make.width.height.mas_equalTo(40);
            make.centerY.equalTo(self.contentView);
        }];
        
        contentLabel = [UILabel new];
        contentLabel.text = @"备注:";
        contentLabel.font = MY_SCALE_RegularFONT(13);
        contentLabel.textColor = MYBlock;
        contentLabel.numberOfLines = 0;
        contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self.contentView addSubview:contentLabel];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel.mas_right).offset(10);
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(selectBtn.mas_left).offset(-10);
        }];
        
        UIView *line = [UIView new];
        line.backgroundColor = MyColor(@"#DEDEE0");
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(0.5);
            make.bottom.equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)update:(NSDictionary *)dic{
    titleLabel.text = dic[@"key"];
    if([dic[@"change"] intValue] == 0){
        selectBtn.hidden = YES;
    }else{
        selectBtn.hidden = NO;
    }
    contentLabel.text = dic[@"value"];
    if([dic[@"state"] intValue] == 2){
        sw.userInteractionEnabled = NO;
        sw.onImage = [UIImage imageNamed:@"com_sw_select"];
        sw.onTintColor = MyColor(@"#ADDAEB");
        [sw setOn:YES];
    }else{
        sw.onTintColor = MYBlue;
        sw.userInteractionEnabled = YES;
        if([dic[@"state"] intValue] == 0){
            sw.onImage = [UIImage imageNamed:@"com_sw_off"];
            [sw setOn:NO];
        }else{
            [sw setOn:YES];
            sw.offImage = [UIImage imageNamed:@"com_sw_on"];
        }
    }
    
}

-(void)btnClick{
    if(self.watermarkEditBlock){
        self.watermarkEditBlock(2);
    }
}
-(void)swchange:(UISwitch *)sender{
    NSInteger state = 1;
    if(sender.isOn){
        state = 1;
    }else{
        state = 0;
    }
    if(self.watermarkEditBlock){
        self.watermarkEditBlock(state);
    }
}
@end
